﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCDto.TMS.MallInterface
{
    public partial class MallOrderDto
    {
        /// <summary>
        /// 获取订单内商品总重量
        /// </summary>
        /// <returns></returns>
        public decimal GetTotalGoodsWeight()
        {
            decimal total = 0;
            if (MallGoodsList != null && MallGoodsList.Any())
                MallGoodsList.ForEach(s => total += (s.GoodsQuantity * s.GoodsWeight));
            return total;
        }

        /// <summary>
        /// 获取订单中最长的那个货物
        /// </summary>
        /// <returns></returns>
        public MallOrderGoodsDto GetMaxLengthGoods()
        {
            if(MallGoodsList!=null && MallGoodsList.Any())
            {
                return MallGoodsList.OrderByDescending(s => s.GetLength()).FirstOrDefault();
            }
            throw new Exception("货物清单为空!");
        }

        /// <summary>
        /// 所有商品都有体积值
        /// </summary>
        /// <returns></returns>
        private bool HasVolume()
        {
            if (MallGoodsList == null || !MallGoodsList.Any())
                return false;
            if (MallGoodsList.Count(s => s.GoodsVolume == null) > 0)
                return false;
            if (MallGoodsList.Count(s => s.GoodsVolume.Value <= 0) > 0)
                return false;
            return true;
        }

        /// <summary>
        /// 获取全部商品总体积
        /// </summary>
        /// <returns></returns>
        public decimal GetTotalGoodsVolume()
        {
            if (HasVolume())
                return MallGoodsList.Sum(s => s.GoodsVolume.Value * s.GoodsQuantity);
            else
                return GetTotalGoodsVolumeByLWH();
        }

        /// <summary>
        /// 获取全部商品总体积 长*宽*高*数量
        /// </summary>
        /// <returns></returns>
        private decimal GetTotalGoodsVolumeByLWH()
        {
            decimal total = 0;
            MallGoodsList.ForEach(s => total += (s.GoodsLength * s.GoodsWidth * s.GoodsHeight * s.GoodsQuantity));
            return total;
        }
    }
}
